<?php

namespace core\lib\paginator;



class FcPaginator
{

    /**
     * Undocumented function
     *
     * @param integer $total
     * @param integer $pagesize
     */
    public function __construct(
        public int $total,
        public int $pagesize,
        public string $url = ''
    ) {}

    /**
     * 
     * @return AdminPaginator
     */
    public function getPaginator(): AdminPaginator|HomePaginator
    {
        if (isFrontPage()) {
            return new HomePaginator(
                $this->total,
                $this->pagesize,
                request()->get('page', 1),
                $this->url == '' ? $this->bulidUrlStr() : $this->url
            );
        }

        return new AdminPaginator(
            $this->total,
            $this->pagesize,
            request()->get('page', 1),
            $this->url == '' ? $this->bulidUrlStr() : $this->url
        );
    }

    public  function bulidUrlStr(): string
    {
        $get = request()->get();
        $get['page'] = '(:num)';

        $str = '?';
        foreach ($get as $p_k => $p_v) {
            $str .=  $p_k . '=' . $p_v . '&';
        }
        return get_now_url() . rtrim($str, '&');
    }
}
